package top.lixunda.ecommerce.server.good.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.ecommerce.server.business.BaseBackupServiceImpl;
import top.lixunda.ecommerce.server.business.api.entity.cache.CacheAdminUser;
import top.lixunda.ecommerce.server.good.api.entity.db.PageInfoShowBackupDO;
import top.lixunda.ecommerce.server.good.api.entity.db.PageInfoShowDO;
import top.lixunda.ecommerce.server.good.mapper.PageInfoShowBackupMapper;
import top.lixunda.ecommerce.server.good.mapper.PageInfoShowMapper;
import top.lixunda.ecommerce.server.good.service.IPageInfoShowService;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 页面数据保存通用结构
 * </p>
 *
 * @author Xumda
 * @since 2020-03-18
 */
@Service
public class PageInfoShowServiceImpl
        extends BaseBackupServiceImpl<PageInfoShowMapper, PageInfoShowDO,
        PageInfoShowBackupMapper, PageInfoShowBackupDO>
        implements IPageInfoShowService {

    /**
     * 数据转换
     *
     * @param pageInfoShowDO 原来的对象
     * @param type           保存类型，删除备份或更新备份
     * @return 缓存备份对象
     */
    @Override
    protected PageInfoShowBackupDO convertData(PageInfoShowDO pageInfoShowDO, String type) {
        PageInfoShowBackupDO pageInfoShowBackupDO = new PageInfoShowBackupDO();
        CacheAdminUser adminUser = this.currentAdminUserCacheNotNull();
        BeanUtils.copyProperties(pageInfoShowDO, pageInfoShowBackupDO);
        pageInfoShowBackupDO.setOptionType(type.equals(PageInfoShowServiceImpl.TYPE_REMOVE)
                ? PageInfoShowBackupDO.TYPE_REMOVE : PageInfoShowBackupDO.TYPE_UPDATE)
                .setOptionUserId(adminUser.getAccount())
                .setOptionUserName(adminUser.getName())
                .setOptionTime(LocalDateTime.now());
        return pageInfoShowBackupDO;
    }

    /**
     * 从对象中获取主键id
     *
     * @param pageInfoShowDO 实体对象
     * @return 主键id
     */
    @Override
    protected Serializable findId(PageInfoShowDO pageInfoShowDO) {
        return pageInfoShowDO.getId();
    }

    /**
     * 插入一条数据
     *
     * @param pageInfoShowDO PageInfoShowDO
     * @return 是否插入成功
     */
    @Override
    public boolean insertPageInfo(PageInfoShowDO pageInfoShowDO) {
        if (ObjectValidators.isEmpty(pageInfoShowDO)) {
            throw getAppException("页面数据不能为空");
        }
        CacheAdminUser adminUser = currentAdminUserCacheNotNull();
        LocalDateTime localDateTime = LocalDateTime.now();
        pageInfoShowDO.setCreateTime(localDateTime)
                .setCreateUserId(adminUser.getAccount())
                .setCreateUserName(adminUser.getName())
                .setCreateUserType(PageInfoShowDO.USER_TYPE_ADMIN)
                .setUpdateUserId(adminUser.getAccount())
                .setUpdateTime(localDateTime)
                .setUpdateUserName(adminUser.getName());
        return this.save(pageInfoShowDO);
    }

    /**
     * 批量插入数据
     *
     * @param list 多个 PageInfoShowDO 数据
     * @return 是否插入成功
     */
    @Override
    public boolean insertPageInfoBatch(List<PageInfoShowDO> list) {
        if (ObjectValidators.isEmpty(list)) {
            throw getAppException("页面数据不能为空");
        }
        CacheAdminUser adminUser = currentAdminUserCacheNotNull();
        LocalDateTime localDateTime = LocalDateTime.now();
        List<PageInfoShowDO> insertList = list.stream()
                .filter(Objects::nonNull)
                .map(pageInfoShowDO -> pageInfoShowDO.setCreateTime(localDateTime)
                        .setCreateUserId(adminUser.getAccount())
                            .setCreateUserName(adminUser.getName())
                            .setCreateUserType(PageInfoShowDO.USER_TYPE_ADMIN)
                        .setUpdateUserId(adminUser.getAccount())
                            .setUpdateTime(localDateTime)
                        .setUpdateUserName(adminUser.getName()))
                .collect(Collectors.toList());
        return !ObjectValidators.isEmpty(insertList) && this.saveBatch(insertList);
    }

    /**
     * 修改一条数据
     *
     * @param pageInfoShowDO 页面数据保存通用结构
     * @return 是否修改成功
     */
    @Override
    public boolean changePageInfoById(PageInfoShowDO pageInfoShowDO) {
        if (ObjectValidators.isEmpty(pageInfoShowDO)) {
            throw getAppException("页面数据不能为空");
        }
        if (ObjectValidators.isEmpty(pageInfoShowDO.getId())) {
            throw getAppException("数据编号不能为空");
        }
        CacheAdminUser adminUser = currentAdminUserCacheNotNull();
        LocalDateTime localDateTime = LocalDateTime.now();
        pageInfoShowDO.setUpdateUserId(adminUser.getAccount())
                .setUpdateTime(localDateTime)
                .setUpdateUserName(adminUser.getName());
        return this.updateById(pageInfoShowDO);
    }

    /**
     * 批量修改数据
     *
     * @param list 页面数据保存通用结构列表
     * @return 是否修改成功
     */
    @Override
    public boolean changePageInfoByIds(List<PageInfoShowDO> list) {
        if (ObjectValidators.isEmpty(list)) {
            throw getAppException("页面数据不能为空");
        }
        CacheAdminUser adminUser = currentAdminUserCacheNotNull();
        LocalDateTime localDateTime = LocalDateTime.now();
        List<PageInfoShowDO> insertList = list.stream()
                .filter(item -> {
                    if (ObjectValidators.isEmpty(item) || ObjectValidators.isEmpty(item.getId())) {
                        throw getAppException("参数或参数编号不能为空");
                    }
                    return true;
                })
                .peek(pageInfoShowDO -> pageInfoShowDO
                        .setUpdateUserId(adminUser.getAccount())
                        .setUpdateTime(localDateTime)
                        .setUpdateUserName(adminUser.getName()))
                .collect(Collectors.toList());
        return !ObjectValidators.isEmpty(insertList) && this.updateBatchById(insertList);
    }

}
